package com.example.bgmachine.bgmachine.handlouts;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.bgmachine.bgmachine.R;
import com.example.bgmachine.bgmachine.homes.HomePages;
import com.example.bgmachine.bgmachine.orders.OrderContentAdapter;


import java.util.LinkedList;

public class HandlContentAdapter extends BaseAdapter {
    private Context mContext;
    private LinkedList<HandlContentData> mData = new LinkedList<HandlContentData>();

    public HandlContentAdapter(Context context, LinkedList<HandlContentData> list) {
        this.mContext = context;
        this.mData = list;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    // txt_pin txt_content
    @Override
    public View getView(int position, View convertViewtms, ViewGroup parent) {
        HandlContentAdapter.ViewHolder holder = null;
        if (convertViewtms == null) {
            View convertView = View.inflate(mContext, R.layout.frament_handl_list_item_content, null);
            convertViewtms= convertView;
            holder = new ViewHolder();

            holder.mHandlContentData = mData.get(position);

            holder.frament_handl_index_textview  = convertView.findViewById(R.id.frament_handl_index_textview);
            holder.frament_handl_menu_textview   = convertView.findViewById(R.id.frament_handl_menu_textview);
            holder.frament_handl_input_edittext   = convertView.findViewById(R.id.frament_handl_input_edittext);

            holder.frament_handl_index_textview.setText( String.valueOf( holder.mHandlContentData.getMenuid() ) );
            holder.frament_handl_menu_textview.setText( holder.mHandlContentData.getMenuName());
            holder.frament_handl_input_edittext.setText(  holder.mHandlContentData.getMenuOutData() );


            holder.frament_handl_list_content_bottom_btn_operation_ok = convertView.findViewById(R.id.frament_handl_list_content_bottom_btn_operation_ok);
            holder.frament_handl_list_content_bottom_btn_operation_ok.setTag( holder );
            holder.frament_handl_list_content_bottom_btn_operation_ok.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {

                    ViewHolder vs = (ViewHolder)v.getTag();

                    if(vs == null){
                        Log.d("aaa", " holder.mOrderContentData.getOrderNumber()==cc=" );
                        return;
                    }
                    if(vs.frament_handl_input_edittext.getText().length() > 0){
                        vs.mHandlContentData.setMenuOutData(vs.frament_handl_input_edittext.getText().toString());
                        HomePages.mHomePages.outHandlData( vs.mHandlContentData.getMenuid(),vs.mHandlContentData.getMenuOutData());

                    }


                }
            });
            holder.frament_handl_list_content_bottom_btn_operation_ok.setEnabled(true);


            holder.frament_handl_list_content_bottom_btn_operation_cancel = convertView.findViewById(R.id.frament_handl_list_content_bottom_btn_operation_cancel);
            holder.frament_handl_list_content_bottom_btn_operation_cancel.setTag( holder );
            holder.frament_handl_list_content_bottom_btn_operation_cancel.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {

                    ViewHolder vs = (ViewHolder)v.getTag();

                    if(vs == null){
                        Log.d("aaa", " holder.mOrderContentData.getOrderNumber()==cc=" );
                        return;
                    }
                    HomePages.mHomePages.updateSetAllowance(vs.mHandlContentData.getMenuid());

                    HomePages.mHomePages.allowanceViewUpdae();
                    HomePages.mHomePages.setParamView();

                }
            });
            holder.frament_handl_list_content_bottom_btn_operation_cancel.setEnabled(true);

            convertView.setTag(holder);
        } else {

            holder = (HandlContentAdapter.ViewHolder) convertViewtms.getTag();
            holder.mHandlContentData = mData.get(position);

            holder.frament_handl_index_textview  = convertViewtms.findViewById(R.id.frament_handl_index_textview);
            holder.frament_handl_menu_textview   = convertViewtms.findViewById(R.id.frament_handl_menu_textview);
            holder.frament_handl_input_edittext   = convertViewtms.findViewById(R.id.frament_handl_input_edittext);

            holder.frament_handl_list_content_bottom_btn_operation_ok = convertViewtms.findViewById(R.id.frament_handl_list_content_bottom_btn_operation_ok);
            holder.frament_handl_list_content_bottom_btn_operation_ok.setTag( holder );
            holder.frament_handl_list_content_bottom_btn_operation_cancel = convertViewtms.findViewById(R.id.frament_handl_list_content_bottom_btn_operation_cancel);
            holder.frament_handl_list_content_bottom_btn_operation_cancel.setTag( holder );


            holder.frament_handl_index_textview.setText( String.valueOf( holder.mHandlContentData.getMenuid() ) );
            holder.frament_handl_menu_textview.setText( holder.mHandlContentData.getMenuName());
            holder.frament_handl_input_edittext.setText(  holder.mHandlContentData.getMenuOutData() );


        }

        return convertViewtms;
    }
    //添加一个元素
    public void add(HandlContentData data) {
        if (mData == null) {
            mData = new LinkedList<>();
        }
        mData.add(data);
        notifyDataSetChanged();
    }

    //往特定位置，添加一个元素
    public void add(int position, HandlContentData data) {
        if (mData == null) {
            mData = new LinkedList<>();
        }
        mData.add(position, data);
        notifyDataSetChanged();
    }

    public void remove(HandlContentData data) {
        if (mData != null) {
            mData.remove(data);
        }
        notifyDataSetChanged();
    }

    public void remove(int position) {
        if (mData != null) {
            mData.remove(position);
        }
        notifyDataSetChanged();
    }

    public void clear() {
        if (mData != null) {
            mData.clear();
        }
        notifyDataSetChanged();
    }


    private class ViewHolder {

        public TextView frament_handl_index_textview  = null;
        public TextView frament_handl_menu_textview   = null;
        public EditText frament_handl_input_edittext   = null;
        public Button frament_handl_list_content_bottom_btn_operation_ok = null;
        public Button frament_handl_list_content_bottom_btn_operation_cancel = null;


        public HandlContentData mHandlContentData = null;
    }


}
